<template>
    <div>
        <Modal :title="modalTitle" v-model="modalShow" :width="modalWidth" :mask-closable="false" :closable="false">
            <div v-show="modalType === 'edit' || modalType === 'add'">
                <Form ref="formItem" :rules="ruleValidate" label-position="right" :model="formItem" :label-width="130">
                    <!--<FormItem label="文章类别：" prop="category_id">
                        <Select v-model="formItem.category_id" placeholder="请选择文章类别" filterable>
                            <Option v-for="(option, index) in categoryObj" :value="option.id" :key="index">{{option.category}}</Option>
                        </Select>
                    </FormItem>-->
                    <FormItem label="场所名称：" prop="place_name">
                        <Input type="text" maxlength="50" v-model="formItem.place_name" placeholder="请输入场所名称" clearable></Input>
                    </FormItem>
                    <FormItem label="场所地址：" prop="place_address">
                        <Input type="textarea" :autosize="{minRows: 2, maxRows: 5}" maxlength="255"
                               v-model="formItem.place_address" placeholder="请输入场所地址" clearable></Input>
                    </FormItem>
                    <FormItem label="经度：" prop="lat">
                        <Input type="text" maxlength="20" v-model="formItem.lat" placeholder="请输入经度" clearable></Input>
                    </FormItem>
                    <FormItem label="纬度：" prop="lan">
                        <Input type="text" maxlength="20"  v-model="formItem.lan" placeholder="请输入纬度" clearable></Input>
                    </FormItem>
                    <FormItem label="联系人姓名：" prop="name">
                        <Input type="text" maxlength="20" v-model="formItem.name" placeholder="请输入联系人姓名" clearable></Input>
                    </FormItem>
                    <FormItem label="联系电话：" prop="mobile">
                        <Input type="text" maxlength="20" v-model="formItem.mobile" placeholder="请输入联系电话" clearable></Input>
                    </FormItem>
                    <FormItem label="场所类别：" prop="place_type">
                        <Input type="text" maxlength="100" v-model="formItem.place_type" placeholder="请输入场所类别" clearable></Input>
                    </FormItem>
                </Form>
            </div>
            <div v-show="modalType === 'info'">
                <Form label-position="right" :model="formItem" :label-width="150">
                    <FormItem label="场所名称：">
                        {{formItem.place_name}}
                    </FormItem>
                    <FormItem label="场所地址：">
                        {{formItem.place_address}}
                    </FormItem>
                    <FormItem label="经度：">
                        {{formItem.lat}}
                    </FormItem>
                    <FormItem label="纬度：">
                        {{formItem.lan}}
                    </FormItem>
                    <FormItem label="联系人姓名：">
                        {{formItem.name}}
                    </FormItem>
                    <FormItem label="联系电话：">
                        {{formItem.mobile}}
                    </FormItem>
                    <FormItem label="场所类别：">
                        {{formItem.place_type}}
                    </FormItem>
                    <FormItem label="二维码：">
                        <img :src="formItem.qr_code" width="200">
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <template v-if="modalType === 'edit' || modalType === 'add'">
                    <Button type="text" @click="cancelOption">取消</Button>
                    <Button type="success" @click="saveOption" :loading="btnLoading">保存</Button>
                </template>
                <template v-if="modalType === 'info'">
                    <Button type="text" @click="cancelOption">关闭</Button>
                </template>
            </div>
        </Modal>
    </div>
</template>

<script>
import {
  updatePlaceInfo
} from '@/api/pass'
import { saveExcelData } from '@/api/worker'
// import { formatDate } from '@/components/dateFormat'
export default {
  name: 'optionModal',
  props: {
    modalTitle: {
      type: String,
      default: () => {
        return ''
      }
    },
    modalType: {
      type: String,
      default: () => {
        return 'info'
      }
    },
    modalShow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    modalWidth: {
      type: Number,
      default: () => {
        return 800
      }
    },
    dataObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      ruleValidate: {
        place_name: [
          { required: true, message: '场所名称不能为空', trigger: 'blur' }
        ],
        place_address: [
          { required: true, message: '场所地址不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '联系人姓名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ]
      },
      btnLoading: false,
      formItem: {},
      categoryObj: {}
    }
  },
  methods: {
    getArticleCategoryList () {
      // getArticleCategoryList().then(ret => {
      //   let data = ret.data
      //   if (data.errcode === 0) {
      //     this.categoryObj = ret.data.data
      //   } else {
      //     this.error(data)
      //   }
      // })
    },
    cancelOption () {
      this.$emit('cancelOption')
    },
    saveOption () {
      let param = {}
      if (this.modalType === 'add') {
        param = this.formItem
        this.$delete(param, 'id')
      } else if (this.modalType === 'edit') {
        param = this.formItem
      }
      // 临时数据处理
      param.type = 'tour'
      // 数据校验
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          // this.formItem.publish_time = formatDate(this.formItem.publish_time, 'yyyy-MM-dd')
          // console.log('提交表单', this.formItem)
          if (this.modalType === 'add') {
            this.addDataAction(param)
          } else if (this.modalType === 'edit') {
            this.updateDataAction(param)
          }
        } else {
          this.$Message.error({
            content: '操作失败,请核对添加信息后重试'
          })
        }
      })
    },
    updateDataAction (param) {
      this.btnLoading = true
      updatePlaceInfo(param).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.btnLoading = false
          this.success()
          this.$emit('saveOption')
        } else {
          this.btnLoading = false
          this.error(data)
        }
      })
    },
    addDataAction (val) {
      this.btnLoading = true
      let arr = []
      const obj = this.$common.xeUtils.clone(val)
      arr.push(obj)
      let param = { type: 'place', dataList: arr }
      saveExcelData(param).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.btnLoading = false
          this.success()
          this.$emit('saveOption')
        } else {
          this.btnLoading = false
          this.error(data)
        }
      })
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    }
  },
  mounted () {
    // this.getArticleCategoryList()
  },
  watch: {
    dataObj (val) {
      this.formItem = val
    }
  },
  computed: {}
}
</script>

<style scoped>
</style>
